import { Elysia } from 'elysia';

import { MayError } from '@/utils/error';
import { fail } from '@/common/result';

/**
 * 错误包装中间件
 *
 * 该中间件用于包装错误，将错误转换为标准的错误结果对象
 */
export const errorWrapperMiddleware = new Elysia({ name: 'error-wrapper' })
  .onError(({ error }) => {
    if (error instanceof MayError) {
      return fail(error.name, error.message);
    }
    return undefined;
  })
  .as('scoped');
